package com.xy.base.reciver;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.provider.Settings;
import android.widget.Toast;

import com.xy.debug.LogUtils;
import com.xy.net.NetConnectUtils;

/**
 * Created by John on 2016/12/12.
 */

public class NetStateReciver extends BroadcastReceiver {
    private static final String TAG = "NetStateReciver";
    private AlertDialog alertDialog = null;
    public static boolean isNeedDialog = false;
    @Override
    public void onReceive(final Context context, Intent intent) {
        NetConnectUtils connectUtils = NetConnectUtils.getInstance(context);
        if (connectUtils.isNetWorkConnet()) {
            switch (connectUtils.getNetWrokType()) {
                case NetConnectUtils.NETWORK_WIFI:
                   LogUtils.i(TAG,"wifi");
                    break;
                default:
                    LogUtils.i(TAG,"移动网络");
                    break;
            }
        } else {
            Toast.makeText(context,"网络未连接，请打开网络",Toast.LENGTH_SHORT).show();
            if (isNeedDialog && alertDialog == null){
                alertDialog = new AlertDialog.Builder(context)
                        .setMessage("是否进行网络设置？")
                        .setPositiveButton("是", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Intent intent = new Intent(Settings.ACTION_QUICK_LAUNCH_SETTINGS);
                                context.startActivity(intent);
                                dialogInterface.dismiss();

                            }
                        }).setNegativeButton("否", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                            }
                        }).create();
            }
            if (alertDialog != null && !alertDialog.isShowing()){
                alertDialog.show();
            }
        }
    }

    public static IntentFilter getIntentFilter(){
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        return filter;
    }

}
